package variable

import (
	"github.com/go-playground/validator/v10"
	"github.com/go-redis/redis"
	ufsdk "github.com/ufilesdk-dev/ufile-gosdk"
	"gorm.io/gorm"
	"os"
	"peilian-api/app/global/config"

	"strings"
)

type Databases struct {
	MySQL *gorm.DB      // MySQL连接句柄
	Redis *redis.Client // Redis连接句柄
}

var (
	BasePath   string                                    // 定义项目的根目录
	MainPath   string            = "/cmd/pufawang"       // 入口文件目录
	RunMode    string            = "dev"                 // 运行模式
	Env        string            = "local"               // 运行环境
	DateFormat string            = "2006-01-02 15:04:05" //  设置全局日期时间格式
	Config     *config.AppConfig                         // 配置文件
	DB         Databases                                 // 数据库连接句柄
	Validator  *validator.Validate
	UfsConfig  *ufsdk.Config
)

func init() {
	Validator = validator.New()
	envPath := os.Getenv("PREFIX")
	envMode := os.Getenv("MODE")
	Env = os.Getenv("ENV")

	if len(envMode) > 0 {
		RunMode = envMode
	}
	if len(envPath) > 0 {
		BasePath = envPath
	} else {
		if curPath, err := os.Getwd(); err == nil {
			if strings.HasSuffix(curPath, MainPath) {
				BasePath = strings.Replace(curPath, MainPath, "", 1)
			} else {
				BasePath = curPath
			}
		}
	}
}
